package com.joplus.housekeeper.ui.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.joplus.housekeeper.ui.view.painningView.PanningView;
import com.joplus.housekeeper.utils.AnalysisUtils;
import com.joplus.housekeeper.utils.L;

/**
 * 自动滚动ImageView和下啦出现登录界面的容器
 * author wutingxi
 * time 2016/8/12 16:20
 */
public class AutoShakeLayout extends FrameLayout {


    String TAG = "AutoShakeLayout";
    int height, widht;
    int start = 0;
    int end = 0;
    int time = 20000;
    private Boolean isStop = false;

    YoYoLine yoyo;
    ViewGroup viewGroup;

    int layoutheight;

    PanningView panningView;


    View backGroudView;
    Context mcontext;

    public AutoShakeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mcontext = context;


    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        Log.d(TAG, "height-->" + height);

        viewGroup = (ViewGroup) getChildAt(3);
        LayoutParams params = (LayoutParams) viewGroup.getLayoutParams();
        params.height = height;
        viewGroup.setLayoutParams(params);
        layoutheight = viewGroup.getMeasuredHeight();
        Log.d(TAG, "layoutheight-->" + layoutheight);
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);

    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();


        panningView = (PanningView) getChildAt(0);
        panningView.startPanning();
        yoyo = (YoYoLine) getChildAt(2);
        ;
        yoyo.setStageCallback(new YoYoLine.StageCallback() {
            @Override
            public void stagePage(int yposition) {
                panningView.stopPanning();
                ObjectAnimator.ofFloat(viewGroup, "translationY", height, height-yposition).setDuration(0).start();
                L.d(TAG, "yposition-->" + yposition);
            }

            @Override
            public void stage(Boolean stage) {
                if (stage) {
                    isStop = true;
                    //去除布局提升UI速度
                    yoyo.setVisibility(GONE);
                    panningView.setVisibility(GONE);

                } else {
                    panningView.startPanning();
                }

            }
        });


    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (!isStop) {
            ObjectAnimator.ofFloat(viewGroup, "translationY", 0, height).setDuration(0).start();
        }

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();


    }


}
